package com.leil.project

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Image
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.StarBorder
import androidx.compose.material.icons.outlined.Folder
import androidx.compose.material.icons.outlined.KeyboardArrowDown
import androidx.compose.material.icons.outlined.KeyboardArrowUp
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.jetbrains.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.HorizontalDivider
import androidx.compose.runtime.derivedStateOf

data class Item(
    val id: Int,
    val description: String,
    val icon: ImageVector,
    val count: String
)

data class Group(
    val id: Int,
    val groupName: String,
    val items: List<Item>,
    val icon: ImageVector,
    val isExpanded: Boolean = false
)

@Composable
expect fun PlatformScrollbar(
    state: LazyListState,
    modifier: Modifier = Modifier,
)

@Composable
fun GroupList(groups: List<Group>) {
    val state = rememberLazyListState()
    val inScroll by remember {
        derivedStateOf { state.canScrollForward || state.canScrollBackward }
    }
    Box(
        modifier = Modifier.fillMaxWidth()
    ) {
        if (inScroll) {
            HorizontalDivider(thickness = 1.dp)
        }
        LazyColumn(
            state = state,
            modifier = Modifier.fillMaxWidth()
        ) {
            items(groups) { group ->
                Group(group)
            }
        }
        if (inScroll) {
            HorizontalDivider(thickness = 1.dp,
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .fillMaxWidth()
                )
        }
        PlatformScrollbar(
            state = state,
            modifier = Modifier.fillMaxHeight().align(Alignment.CenterEnd)
        )
    }
}

@Composable
fun Group(group: Group) {
    var visible by remember { mutableStateOf(group.isExpanded) }
    Column(
        modifier = Modifier.fillMaxWidth(),
    ) {
        Row(
            modifier = Modifier.fillMaxWidth().clickable { visible = !visible }
                .padding(horizontal = 8.dp, vertical = 5.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Icon(group.icon, contentDescription = "Group")
                Spacer(modifier = Modifier.width(10.dp))
                Text(text = group.groupName)
            }
            Icon(
                imageVector = if (visible) Icons.Outlined.KeyboardArrowUp else Icons.Outlined.KeyboardArrowDown,
                contentDescription = "TipArrow"
            )
        }
        AnimatedVisibility(
            visible = visible,
        ) {
            Column {
                group.items.forEach {
                    Row(
                        modifier = Modifier.fillMaxWidth()
                            .padding(horizontal = 8.dp, vertical = 5.dp),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                        ) {
                            Spacer(modifier = Modifier.width(20.dp))
                            Icon(it.icon, contentDescription = "ProjectIcon")
                            Spacer(modifier = Modifier.width(10.dp))
                            Text(text = it.description)
                        }
                        Box(
                            modifier = Modifier
                                .size(24.dp)
                                .background(
                                    MaterialTheme.colorScheme.primaryContainer,
                                    shape = CircleShape
                                )
                                .clip(CircleShape),
                            contentAlignment = Alignment.Center,
                        ) {
                            Text(
                                it.count,
                                style = MaterialTheme.typography.bodySmall.copy(fontSize = 12.sp)
                            )
                        }
                    }
                }
            }
        }
    }
}


@Preview
@Composable
fun GroupListPreview() {
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        val groups = listOf<Group>(
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup(),
            createGroup()
        )
        GroupList(groups)
    }
}

fun createGroup(): Group {
    return Group(
        id = 1,
        groupName = "项目工作",
        items = listOf(
            Item(
                id = 1,
                description = "项目1",
                icon = Icons.Default.Image,
                count = "1"
            ),
            Item(
                id = 2,
                description = "项目2",
                icon = Icons.Default.Star,
                count = "2"
            ),
            Item(
                id = 3,
                description = "项目3",
                icon = Icons.Default.StarBorder,
                count = "5"
            )
        ),
        icon = Icons.Outlined.Folder,
        isExpanded = false
    )
}